#!/bin/ksh
#(C) COPYRIGHT International Business Machines Corp. 1991, 1992, 1993.
# All Rights Reserved.  Licensed Materials - Property of IBM
#####################################################################
# Note: The MPNP Config program looks for the file helvR14*.Z in
#       directory /usr/lib/X11/fonts/75dpi .  If this file is not
#       present in the directory then the MPNP program will not be
#       activated.
#####################################################################
# 30-Jan-95 (SLL) Removed use of checkDisplay for VisualWorks 2.0

waiting=true
count=0
ip=$1
while [ "$waiting" = true ]
do
        if [ -s "cfgargs" ]
        then waiting=true
        else waiting=false
        fi
        if  [ "$waiting" = true ]
        then
                sleep 2
                count=`expr $count + 1`
                if [ "$count" -gt 3 ]
                then waiting=false
                fi
        fi
done

#####################################################################
# Check if we have helvetica 14-point installed in the X11 font
# directories.  Write the command line arguments into the file,
# cfgargs.  The Configuration program will read it and then
# delete it when it comes up.  Invoke the Configuration program
# in the current directory.
function processit {
#   if [ -f /usr/lib/X11/fonts/75dpi/helvR14.snf.Z ]; then
   if [ -n "$(ls -l /usr/lib/X11/fonts/75dpi/helvR14*.Z)" ]; then
        if [ -s "cfgargs" ]; then
           rm cfgargs
        fi
        echo $ip >> cfgargs "\n"
        ./cfg.aix cfg.app&
   fi
exit 0
}

#####################################################################
# Check if the files cfg.aix cfg.app, cfg.hlp, cfg.msg and cfg.lgo are installed.
# If not then display an error message and exit.
# Also make sure the current directory is writable.
function checkForAppFiles {

        if [ ! -w . ]; then
                echo '\007'
                echo 'MPNP error: The current directory is not writable.  Please'
                echo 'either make the current directory writable or copy the'
                echo 'configuration program to a writable directory and try again.'
                exit 1
        fi
        if [ ! -r ./cfg.hlp ]; then
                echo '\007'
                echo 'MPNP error: The file cfg.hlp which contains the online help can'
                echo '  not be found in the current directory.  Please re-install the'
                echo '  MPNP Configuration Program.'
                exit 1
        fi
        if [ ! -r ./cfg.msg ]; then
                echo '\007'
                echo 'MPNP error: The file cfg.msg which contains the system messages can'
                echo '  not be found in the current directory.  Please re-install the'
                echo '  MPNP Configuration Program.'
                exit 1
        fi
        if [ ! -r ./cfg.lgo ]; then
                echo '\007'
                echo 'MPNP error: The file cfg.lgo which contains the program logo can'
                echo '  not be found in the current directory.  Please re-install the'
                echo '  MPNP Configuration Program.'
                exit 1
        fi
        if [ ! -r ./cfg.aix ]; then
                echo '\007'
                echo 'MPNP error: The file cfg.aix can not be found in the current'
                echo '  directory.  Please re-install the MPNP Configuration Program.'
                exit 1
        fi
        if [ ! -r ./cfg.app ]; then
                echo '\007'
                echo 'MPNP error: The file cfg.app not be found in the current directory.'
                echo '  Please re-install the MPNP Configuration Program.'
                exit 1
        fi
}

#####################################################################
# Check if we have the commands, csh, dosdir, dosread and doswrite
# installed in /bin or /usr/bin.  If not then display an error
# message and exit.
function checkForDOSIOFiles {
        hasCsh=0
        hasDosDir=0
        hasDosRead=0
        hasDosWrite=0
        if [ -d /bin ]; then
                if [ ! -x /bin/csh ]; then
                        exitWithNoDOSIOFiles
                else
                        hasCsh=1
                fi
                if [ ! -r /bin/dosdir ]; then
                        exitWithNoDOSIOFiles
                else
                        hasDosDir=1
                fi
                if [ ! -x /bin/dosread ]; then
                        exitWithNoDOSIOFiles
                else
                        hasDosRead=1
                fi
                if [ ! -x /bin/doswrite ]; then
                        exitWithNoDOSIOFiles
                else
                        hasDosWrite=1
                fi
        fi
        if [ -d /usr/bin ]; then
                if [ "$hasCsh" = 0 -a ! -x /usr/bin/csh ]; then
                        exitWithNoDOSIOFiles
                fi
                if [ "$hasDosDir" = 0 -a ! -x /usr/bin/dosdir ]; then
                        exitWithNoDOSIOFiles
                fi
                if [ "$hasDosRead" = 0 -a ! -x /usr/bin/dosread ]; then
                        exitWithNoDOSIOFiles
                fi
                if [ "$hasDosWrite" = 0 -a ! -x /usr/bin/doswrite ]; then
                        exitWithNoDOSIOFiles
                fi
        fi

}

#####################################################################
# Display the error message about the missing files that are
# currently being used by the Configuration program to read and
# write the IML file to and from the DOS formatted (1.44MB) diskette.
function exitWithNoDOSIOFiles
{
        echo '\007'
        echo 'MPNP error: The AIX commands, dosdir, dosread, doswrite and csh'
        echo '  can not be found in the directories /bin or /usr/bin.  Please'
        echo '  install these commands on your system before trying to run the'
        echo '  MPNP Configuration Program.'
        exit 1
}

#####################################################################
# Check for the setting of the DISPLAY variable. If it is set to
# unix:0 or :0, then reset to the hostname of the machine this
# script is running on.
function checkDisplay {
      blank=''
      prefix=`echo $DISPLAY | awk -F':' '{print $1}'`
      suffix=`echo $DISPLAY | awk -F':' '{print $2}'`
      case $suffix in
       $blank) suffix=0.0 ;;
      esac
      /usr/bin/X11/xhost +`uname -n` > /dev/null
      case $prefix in
       unix) DISPLAY=`uname -n`:$suffix ; export DISPLAY ;;
       $blank) DISPLAY=`uname -n`:$suffix ; export DISPLAY ;;
      esac
}

#####################################################################
# Check if the X11 font files for 14-point helvetica, times and new
# century schoolbook are installed.  If not display an error message
# and exit.
if [ -d /usr/lib/X11/fonts/75dpi ]; then
   if [ -n "$(ls -l /usr/lib/X11/fonts/75dpi/helv*.Z)" ]; then
      if [ -n "$(ls -l /usr/lib/X11/fonts/75dpi/tim*.Z)" ]; then
        if [ -n "$(ls -l /usr/lib/X11/fonts/75dpi/ncen*.Z)" ]; then
                checkForAppFiles
                checkForDOSIOFiles
#               checkDisplay
                processit
        fi
      fi

# They have some helvetica font so let the user continue
      checkForAppFiles
      checkForDOSIOFiles
#     checkDisplay
      processit
   fi
fi
echo '\007'
echo 'MPNP Error : The X11 font files for Helvetica, Times and New CenturySchoolbook'
echo '  could not be found in the X11 font directory /usr/lib/X11/fonts/75dpi.  Please'
echo '  install these fonts before trying to run the MPNP Configuration Program.'
exit 1
